var HashMapArray = require("./utils/HashMapArray.js");
var Product = require("./Product.js");

var exports = {
	ver: "1.0.0",
	createTime: new Date().dateFormat('yyyyMMDDHHmmss'),
	stage: Design.paper.g(),
	editCon: Design.paper.g(),
	product: null,
	translate: {
		x: 0,
		y: 0
	}
};


exports.init = function() {
	//layer.load();
	exports.editCon.appendTo(exports.stage);
	exports.editCon.attr("displayType", "face-list")
	Design.tool.group.appendTo(exports.stage);

	initKey();

	$("#dzg").on("click", function() {
		$(".chooseProduct").fadeOut();
		setTimeout(function() {
			$(".chooseProduct").fadeIn();
			layer.msg("请选择产品");
		}, 500)
	});
}

exports.setJson = function(data) {
	exports.ver = data.ver;
	exports.createTime = data.createTime;
	var productdata = data.product;
	exports.addProduct(productdata);
}

//添加修改产品
exports.addProduct = function(data) {
	if (this.product) {
		this.product.destroy();
		this.product = null;
	}
	this.product = new Product(exports.editCon);
	if (data) {
		this.product.setJson(data);
	}
	$("#dzg").hide();
}

exports.currFace = function() {
	return this.product.currFace;
}

exports.addElement = function(data) {
	if (this.product.currFace) {
		this.product.currFace.addElement(data);
	}
}

exports.removeElement = function(id) {
	if (this.product.currFace) {
		this.product.currFace.removeElement(id);
	}
}

exports.getEditeRect = function() {
	var rect = null;
	if (this.product && this.product.currFace) {
		rect = this.product.currFace.getEditeRect();
	} else {
		layer.alert("请先添加产品!");
	}
	return rect;
}

/*
 * 获取保存的数据
 */
exports.getSaveData = function() {
	var self = exports;
	var json = {
		ver: self.ver,
		createTime: self.createTime,
		updateTime: new Date().dateFormat('yyyyMMDDHHmmss'),
		product: self.product.getSaveData()
	}
	return json;
}



exports.moveTo = function(x, y) {
	this.stage.attr({
		"transform": "translate(" + x + "," + y + ")"
	});
	this.translate.x = x;
	this.translate.y = y;
	Design.tool.prexy(x, y);
	this.stage.attr("visibility", "visible");
	layer.closeAll();
}

function initKey() {
	$(document).keydown(function(event) {
		var ele = Design.tool.getTarget();
		if (event.keyCode != 83) {
			if (!ele) return;
		}
		var update = false;
		var moved = 1;
		var matrix = ele._transform.matrix;
		switch (event.keyCode) {
			//up
			case 38:
				update = true;
				matrix.translate(0, -moved);
				break;
				//down
			case 40:
				update = true;
				matrix.translate(0, moved);
				break;
				//left
			case 37:
				update = true;
				matrix.translate(-moved, 0);
				break;
				//right
			case 39:
				update = true;
				matrix.translate(moved, 0);
				break;
			case 81:
				//ctrl+Q 顺时针45°旋转
				if (event.ctrlKey) {
					update = true;
					ele.rotation(45);
				}
				//alt+Q 顺时针5°旋转
				else if (event.altKey) {
					update = true;
					ele.rotation(5);
				}
				break;
				//Z
			case 90:
				var w = ele._transform.width;
				var h = ele._transform.height;
				var obj = ele.element.matrix.split();
				var dd = 0.1;
				var dx = w * obj.scalex * dd;
				var dy = h * obj.scaley * dd;
				//ctrl+Z 放大
				if (event.ctrlKey) {
					update = true;
					event.preventDefault();
					event.stopPropagation();
					matrix.scale(1 + dd, 1 + dd);
					matrix.translate(-dx, -dy);
				}
				//alt+Z 缩小
				else if (event.altKey) {
					update = true;
					event.preventDefault();
					event.stopPropagation();
					matrix.scale(1 - dd, 1 - dd);
					matrix.translate(dx, dy);
				}
				break;
				//delete
			case 46:
				Design.mainEditer.removeElement(ele.id);
				break;
				//s 保存
			case 83:
				if (event.ctrlKey) {
					update = true;
					event.preventDefault();
					event.stopPropagation();
					$(".shoppingCartBtn").click();
				}
				//alt+s 重新设计
				else if (event.altKey) {
					Design.mainEditer.product.reset();
				}
				break;
				//x 图案最大化
			case 88:
				if (event.ctrlKey) {
					$(".imgFull").click();
				}
				break;
				//b 
			case 66:
				//图案宽度最大化
				if (event.ctrlKey) {
					$(".patternWidthMax").click();
				}
				//图案高度最大化
				else if (event.altKey) {
					$(".patternHeightMax").click();
				}
				break;
				//b 
			case 67:
				//图案宽度最大化
				if (event.shiftKey) {
					$(".copyLayer").click();
				}
				break;
			default:
				break;
		}

		if (update) {
			ele = Design.tool.getTarget();
			if (ele) {
				var tm = matrix;
				var newMatrix = new Snap.Matrix(tm.a, tm.b, tm.c, tm.d, tm.x, tm.y);
				if (ele.dmatrix) {
					var dinv = ele.dmatrix.clone();
					dinv.invert();
					newMatrix.add(dinv.a, dinv.b, dinv.c, dinv.d, dinv.x, dinv.y);
				}
				ele.element.transform(newMatrix);
				Design.tool.setTarget(ele);
			}
		}
	});
}

module.exports = exports;
